package com.koro.tools;

import com.koro.annotation.ExcelColumn;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.*;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @author HQ
 */
public class ExcelUtils {

    public static <T> List<T> readExcel(String filePath, Class<T> clazz) throws IOException {
        // 使用类加载器获取资源流
        InputStream inputStream = ExcelUtils.class.getClassLoader().getResourceAsStream(filePath);
        if (inputStream == null) {
            throw new FileNotFoundException("文件未找到: " + filePath);
        }

        List<T> result = new ArrayList<>();
        try (inputStream) {
            Workbook workbook = new XSSFWorkbook(inputStream);
            Sheet sheet = workbook.getSheetAt(0); // 获取第一个工作表
            Iterator<Row> rowIterator = sheet.iterator();

            if (rowIterator.hasNext()) {
                Row headerRow = rowIterator.next(); // 跳过表头行
                while (rowIterator.hasNext()) {
                    Row row = rowIterator.next();
                    T obj = mapRowToEntity(row, clazz, headerRow);
                    result.add(obj);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
        }
        return result;
    }

    private static <T> T mapRowToEntity(Row row, Class<T> clazz, Row headerRow) throws Exception {
        T obj = clazz.getDeclaredConstructor().newInstance();
        DataFormatter formatter = new DataFormatter();

        for (int i = 0; i < headerRow.getLastCellNum(); i++) {
            Cell cell = row.getCell(i);
            String headerValue = headerRow.getCell(i).getStringCellValue();

            for (Field field : clazz.getDeclaredFields()) {
                if (field.isAnnotationPresent(ExcelColumn.class)) {
                    ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
                    if (annotation.value().equals(headerValue)) {
                        field.setAccessible(true);

                        if (cell != null) {
                            String cellValue = formatter.formatCellValue(cell);

                            if (field.getType() == String.class) {
                                field.set(obj, cellValue);
                            } else if (field.getType() == int.class || field.getType() == Integer.class) {
                                try {
                                    field.set(obj, Integer.parseInt(cellValue));
                                } catch (NumberFormatException ignored) {}
                            } else if (field.getType() == double.class || field.getType() == Double.class) {
                                try {
                                    field.set(obj, Double.parseDouble(cellValue));
                                } catch (NumberFormatException ignored) {}
                            } else if (field.getType() == boolean.class || field.getType() == Boolean.class) {
                                field.set(obj, Boolean.parseBoolean(cellValue));
                            }
                        }

                        break;
                    }
                }
            }
        }
        return obj;
    }




}
